﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.Core;
using Kingdee.BOS.Orm.DataEntity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn;
using Kingdee.BOS.Util;
using System.ComponentModel;

namespace LiMinCRMPlugin
{
    /// <summary>
    /// 类名或功能模块简述
    /// </summary>
    /// <remarks>
    /// 功能：改造生产汇报单，增加子单据体，在生产订单下推生产汇报单时，自动根据生产订单拉取原始数据录入表批次信息，指定到对应的子单据体中
    /// 创建人：张虎
    /// 创建日期：2024/6/5 10:36:31
    /// 修改信息（1）：张虎，2024/6/5 10:36:31，修改内容
    /// </remarks>
    /// 
    [Description("在生产订单下推生产汇报单时，自动根据生产订单拉取原始数据录入表批次信息，指定到对应的子单据体中")]
    [HotUpdate]
    public class MOToMORPT: AbstractConvertPlugIn
    {
        public override void OnFieldMapping(FieldMappingEventArgs e)
        {
            base.OnFieldMapping(e);
            List<Kingdee.BOS.Orm.DataEntity.DynamicObject> SrcData = e.ConvertSource as List<Kingdee.BOS.Orm.DataEntity.DynamicObject>;
        }
        /// <summary>
        /// 设置OnFieldMapping 事件启用
        /// </summary>
        /// <param name="e"></param>
        public override void OnBeforeFieldMapping(BeforeFieldMappingEventArgs e)
        {
            base.OnBeforeFieldMapping(e);
            e.FireFieldMappingEvent = true;
        }

        public override void AfterConvert(AfterConvertEventArgs e)
        {
            base.AfterConvert(e);
            ExtendedDataEntity[] heads = e.Result.FindByEntityKey("FBillHead");
            ExtendedDataEntity[] FEntity = e.Result.FindByEntityKey("FEntity");//明细数据包
            SubEntryEntity subEntity = e.TargetBusinessInfo.GetEntity("F_QLTK_RawDate") as SubEntryEntity;//子单据体数据包
            foreach (ExtendedDataEntity head in heads)
            {
                foreach (ExtendedDataEntity ENTRY in FEntity)
                {
                    DynamicObjectCollection dycDetailEntry = ENTRY.DataEntity["FRawDateEntity"] as DynamicObjectCollection;//原始数据录入表单据体数据包
                    string FMATERIALID = ENTRY["MaterialId_Id"].ToString();//物料编码
                    string MoBilINO = ENTRY["MoBillNo"].ToString();//生产订单编号
                    string MOENTRYID = ENTRY["MOENTRYID"].ToString();//生产订单明细行ID
                    string sql = string.Format(@"select a.FID,b.F_DEMO_TEXT,b.F_Dev_FMATERIALID,c.* from demo_t_Cust_Entry100521 a --原始数据录入表单据头
                    left join demo_t_Cust_Entry100522 b on a.FID = b.FID --原始数据录入表单据体
                    left join demo_t_Cust_Entry100524 c on a.FID = c.FEntryID --原始数据录入表子单据体
                    where  a.FDocumentStatus='C' and F_DEMO_TEXT='" + MoBilINO + "' and b.F_QLTK_MOSRCENTRYID='" + MOENTRYID + "'");
                    DataTable sql_toStirng = DBUtils.ExecuteDataSet(this.Context, sql).Tables[0];
                    if (sql_toStirng.Rows.Count > 0)
                    {
                        for (int i = 0; i < sql_toStirng.Rows.Count; i++)
                        {
                            //创建子单据体行数据包
                            DynamicObject newRow = new DynamicObject(subEntity.DynamicObjectType);
                            string FDetailID = sql_toStirng.Rows[i]["FDetailID"].ToString();
                            string F_QLTK_BatchLot = sql_toStirng.Rows[i]["F_DEMO_TEXT1"].ToString();//投料批号
                            double F_QLTK_Number = double.Parse(sql_toStirng.Rows[i]["F_DEMO_DECIMAL1"].ToString());//数量
                            string F_QLTK_SealNumber = sql_toStirng.Rows[i]["F_DEMO_TEXT5"].ToString();//投料批号印章号
                            double F_QLTK_REPORTCOUNT = double.Parse(sql_toStirng.Rows[i]["F_QLTK_REPORTCOUNT"].ToString());//生产汇报数量
                            double number = F_QLTK_Number - F_QLTK_REPORTCOUNT;
                            newRow["F_QLTK_BatchLot"] = F_QLTK_BatchLot;
                            newRow["F_QLTK_Number"] = number;
                            newRow["F_QLTK_SealNumber"] = F_QLTK_SealNumber;
                            newRow["F_QLTK_SrcEntryId"] = FDetailID;
                            dycDetailEntry.Add(newRow);
                        }
                    }
                }
            }
        }
    }
}
